
package com.hlkj.pay.service.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.io.File;

import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.dto.order.SchemeOrderDto;
import com.hlkj.pay.infrastructure.model.common.CommonAppDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;

/**
 * @author HlpayTeam
 * @date 2024/12/30 17:42
 * 参考
 * https://github.com/binarywang/weixin-java-mp-demo/blob/master/src/main/java/com/github/binarywang/demo/wx/mp/config/WxMpConfiguration.java
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Extension(bizId = "1")
public class WechatMpAppServiceService implements WechatAppExtPt {

    private final WxMpService wxMpService;
    @Override
    public CommonResult<SchemeOrderDto> urlLink(CommonAppDO commonAppDO, PayOrderDO payOrderDO, String page) {
        return CommonResult.success();
    }

    @Override
    public CommonResult<WxMaJscode2SessionResult> wxAppUserInfo(CommonAppDO commonAppDO, String code) {
        if (!wxMpService.switchover(commonAppDO.getAppId())) {
            intiConfig(commonAppDO);
            WxMaConfigHolder.set(commonAppDO.getAppId());
        }
        try {
            WxMpUser wxMpUser = wxMpService.getUserService().userInfo(code);
            WxMaJscode2SessionResult wxMaJscode2SessionResult=new WxMaJscode2SessionResult();
            wxMaJscode2SessionResult.setOpenid(wxMpUser.getOpenId());
            wxMaJscode2SessionResult.setUnionid(wxMpUser.getUnionId());
            return CommonResult.success(wxMaJscode2SessionResult);
        }
        catch (WxErrorException e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(1, e.getMessage());
        }
        finally {
            WxMaConfigHolder.remove();// 清理ThreadLocal
        }
    }

    @Override
    public CommonResult<File> getwxacodeunlimit(CommonAppDO commonAppDO, String page, String scene) {
        return CommonResult.success();
    }

    void intiConfig(CommonAppDO commonAppDO) {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl ();
//                WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
        // 使用上面的配置时，需要同时引入jedis-lock的依赖，否则会报类无法找到的异常
        config.setAppId(commonAppDO.getAppId());
        config.setSecret(commonAppDO.getAppSecret());
        // config.setToken(a.sett());
        // config.setAesKey(a.getAesKey());
        wxMpService.addConfigStorage(commonAppDO.getAppId(), config);
    }
}
